//
//  ZJSCNoteTableViewCell.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/5/4.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJSCNoteTableViewCell.h"
#import "ZJShareManager.h"
@interface ZJSCNoteTableViewCell ()
/** 内容 */
@property (nonatomic, strong) UILabel *titleLabel;
/** 评论视频时间 */
@property (nonatomic, strong) UILabel *videoTimeLabel;
/** 时间 */
@property (nonatomic, strong) UILabel *timeLabel;
/** button分割线 */
@property (nonatomic, strong) UILabel *spaceLine1;
/** button分割线 */
@property (nonatomic, strong) UILabel *spaceLine2;
/** 删除 */
@property (nonatomic, strong) UIButton *delete;
/** 分享 */
@property (nonatomic, strong) UIButton *share;
/** 下划线 */
@property (nonatomic, strong) UILabel *underLine;

/** 分享模块 */
@property (nonatomic, strong) ZJShareManager *shareManager;
@end

@implementation ZJSCNoteTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.videoTimeLabel];
        [self.contentView addSubview:self.timeLabel];
        [self.contentView addSubview:self.spaceLine1];
        [self.contentView addSubview:self.spaceLine2];
        [self.contentView addSubview:self.delete];
        [self.contentView addSubview:self.share];
        [self.contentView addSubview:self.underLine];

        self.titleLabel.sd_layout
        .leftSpaceToView(self.contentView, 12 * [UIScreen zj_screenWScale])
        .widthIs([UIScreen zj_screenWith] - 24 * [UIScreen zj_screenWScale])
        .topSpaceToView(self.contentView, 8 *[UIScreen zj_screenHScale])
        .heightIs(10);
        
        self.videoTimeLabel.sd_layout
        .topSpaceToView(self.titleLabel,8 * [UIScreen zj_screenHScale])
        .leftEqualToView(self.titleLabel)
        .widthIs(252 * [UIScreen zj_screenWScale])
        .heightIs([NSString heightWithTextFontSize:12 * [UIScreen zj_textFontScale]]);
        
        self.timeLabel.sd_layout
        .topEqualToView(self.videoTimeLabel)
        .leftSpaceToView(self.videoTimeLabel, 0)
        .rightEqualToView(self.titleLabel)
        .heightRatioToView(self.videoTimeLabel, 1);
        
        self.spaceLine1.sd_layout
        .leftSpaceToView(self.contentView, 0)
        .rightSpaceToView(self.contentView, 0)
        .topSpaceToView(self.videoTimeLabel, 14 * [UIScreen zj_screenHScale])
        .heightIs(1);
        
        self.delete.sd_layout
        .topSpaceToView(self.spaceLine1, 0)
        .leftSpaceToView(self.contentView, 0)
        .widthIs(([UIScreen zj_screenWith] - 1) / 2)
        .heightIs(43 * [UIScreen zj_screenHScale]);
        
        self.spaceLine2.sd_layout
        .topEqualToView(self.delete)
        .leftSpaceToView(self.delete, 0)
        .heightRatioToView(self.delete, 1)
        .widthIs(1);
        
        self.share.sd_layout
        .topEqualToView(self.delete)
        .leftSpaceToView(self.spaceLine2, 0)
        .widthRatioToView(self.delete, 1)
        .heightRatioToView(self.delete, 1);
        
        self.underLine.sd_layout
        .topSpaceToView(self.delete, 0)
        .leftSpaceToView(self.contentView, 0)
        .rightSpaceToView(self.contentView, 0)
        .heightIs(7.5 * [UIScreen zj_screenHScale]);
        
        
    }
    return self;
}

#pragma mark - 点击事件
- (void)action_DeleteButton:(UIButton *)button{
    __weak typeof(self) weakSelf = self;
    [self netWorkingDeleNoteSuccess:^{
        if (weakSelf.delegate) {
            [weakSelf.delegate ZJSCNoteTableViewCellDelegate_DeleteButton:weakSelf.model];
        }
    }];
    
}
//分享
- (void)action_ShareButton:(UIButton *)button{
    ZJShareModel *model = [ZJShareModel new];
    model.title = self.model.NOTE_CONTENT;
    //赋值
    self.shareManager.shareModel = model;
    [self.shareManager show];
}


//删除笔记的网络请求
- (void)netWorkingDeleNoteSuccess:(void(^)(void))success{
    [YJProgressHUD show:@"" inView:[self topViewController].view mode:YJProgressModeLoading];
    NSDictionary *dict = @{@"NOTE_ID":self.model.NOTE_ID};
    [NetWorkingManager postWithUrlString:[NetWorkingURL delCourseNote] Parameter:dict Success:^(id object) {
        [YJProgressHUD hide];
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//删除成功
            success();
        }else{
            [YJProgressHUD showMsgWithoutView:@"删除失败"];
        }
        
    } Fail:^(id object) {
            [YJProgressHUD showMsgWithoutView:@"删除失败"];
    }];
}

#pragma mark - Setter
- (void)setModel:(ZJSCNoteModel *)model{
    _model = model;
    self.timeLabel.text = [NSDateFormatter localStringWithDate:[NSDateFormatter dateTimeWithString:model.CREATED_TIME]];
    self.videoTimeLabel.text = [NSString stringWithFormat:@"视频时间节点 : %@",[NSString getTimeLabelValues:[model.VIDEO_STAMP doubleValue]]];
    
    self.titleLabel.text = model.NOTE_CONTENT;
    self.titleLabel.sd_layout
    .leftSpaceToView(self.contentView, 12 * [UIScreen zj_screenWScale])
    .widthIs([UIScreen zj_screenWith] - 24 * [UIScreen zj_screenWScale])
    .topSpaceToView(self.contentView, 8 *[UIScreen zj_screenHScale])
    .heightIs([self.titleLabel getLableSizeWithMaxWidth:[UIScreen zj_screenWith] - 24 * [UIScreen zj_screenWScale]].height);

    [self.titleLabel updateLayout];
    
}


#pragma mark - Getters
- (UILabel *)titleLabel{
    if (_titleLabel == nil){
        _titleLabel = [UILabel new];
        _titleLabel.textColor = [UIColor textBlackColor];
        _titleLabel.font = [UIFont titleTextFont];
        _titleLabel.numberOfLines = 0;
        _titleLabel.lineSpace = 5 * [UIScreen zj_screenHScale];
        _titleLabel.characterSpace = 2 * [UIScreen zj_screenWScale];
    }
    return _titleLabel;
}

- (UILabel *)videoTimeLabel{
    if (_videoTimeLabel == nil){
        _videoTimeLabel = [UILabel new];
        _videoTimeLabel.textColor = [UIColor zj_colorWithHexString:@"a8a8a8"];
        _videoTimeLabel.font = [UIFont systemFontOfSize:12 * [UIScreen zj_textFontScale]];
    }
    return _videoTimeLabel;
}
- (UILabel *)timeLabel{
    if (_timeLabel == nil){
        _timeLabel = [UILabel new];
        _timeLabel.textColor =  [UIColor zj_colorWithHexString:@"a8a8a8"];
        _timeLabel.font = [UIFont systemFontOfSize:12 * [UIScreen zj_textFontScale]];
        _timeLabel.textAlignment = NSTextAlignmentRight;
    }
    return _timeLabel;
}

- (UILabel *)spaceLine1{
    if (_spaceLine1 == nil){
        _spaceLine1 = [UILabel new];
        _spaceLine1.backgroundColor = [UIColor viewBGGrayColor];
        
    }
    return _spaceLine1;
}

- (UILabel *)spaceLine2{
    if (_spaceLine2 == nil){
        _spaceLine2 = [UILabel new];
        _spaceLine2.backgroundColor = [UIColor viewBGGrayColor];
        
    }
    return _spaceLine2;
}
- (UIButton *)delete{
    if (_delete == nil){
        _delete = [UIButton new];
        _delete.titleLabel.font = [UIFont titleTextFont];
        [_delete setImage:[UIImage imageNamed:@"learn_note_del"] forState:UIControlStateNormal];
        [_delete setTitle:@" 删除" forState:UIControlStateNormal];
        [_delete setTitleColor:[UIColor zj_colorWithHexString:@"a8a8a8"] forState:UIControlStateNormal];
        
        [_delete addTarget:self action:@selector(action_DeleteButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _delete;
}
- (UIButton *)share{
    if (_share == nil){
        _share = [UIButton new];
        _share.titleLabel.font = [UIFont titleTextFont];
        [_share setImage:[UIImage imageNamed:@"learn_note_share"] forState:UIControlStateNormal];
        [_share setTitle:@" 分享" forState:UIControlStateNormal];
        [_share setTitleColor:[UIColor themeColor] forState:UIControlStateNormal];
        [_share addTarget:self action:@selector(action_ShareButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _share;
}
- (UILabel *)underLine{
    if (_underLine == nil){
        _underLine = [UILabel new];
        _underLine.backgroundColor = [UIColor viewBGGrayColor];
        
    }
    return _underLine;
}

- (ZJShareManager *)shareManager{
    if (_shareManager == nil){
        _shareManager = [ZJShareManager new];
    }
    return _shareManager;
}

@end
